home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ForCLI / sum.lha / sum.c < prev    next >
C/C++ Source or Header  |  1994-04-03  |  4KB  |  160 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <functions.h>
  5.  
  6.  
  7. void CloseAll(char *s);
  8. int sumfile(char *filename);
  9.  
  10. char ver[] = "$VER: sum 1.1 (3.4.94) © by Oliver Kaufmann";
  11.  
  12. char *usage = "usage: sum [-h|-?] [<filepatternlist>] [-p<pos>] [-q]\n\
  13.                 -h : show usage\n\
  14.                 -? : show usage\n\
  15.  <filepatternlist> : one or more files or filepatterns,\n\
  16.                      if omitted, use stdin as input\n\
  17.            -p<pos> : start at position <pos> in every line\n\
  18.                 -q : quiet, do not display verbose information\n";
  19.  
  20.  
  21. struct MyAnchorPath {
  22.     struct AChain    *ap_Base;    /* pointer to first anchor */
  23.     struct AChain    *ap_Last;    /* pointer to last anchor */
  24.     LONG    ap_BreakBits;        /* Bits we want to break on */
  25.     LONG    ap_FoundBreak;        /* Bits we broke on. Also returns ERROR_BREAK */
  26.     BYTE    ap_Flags;        /* New use for extra word. */
  27.     BYTE    ap_Reserved;
  28.     WORD    ap_Strlen;        /* This is what ap_Length used to be */
  29.     struct    FileInfoBlock ap_Info;
  30.     UBYTE    ap_Buf[256];        /* Buffer for path name, allocated by user */
  31. };
  32.  
  33. #define MAXLEN 1024
  34.  
  35. struct FileInfoBlock *fib;
  36. struct MyAnchorPath myap;
  37. struct AnchorPath *ap;
  38.  
  39. unsigned long sum=0, n=0;
  40. int pos=0;
  41. int quietflag = 0;
  42. char h[MAXLEN];
  43.  
  44. void main(int argc, char *argv[])
  45. {
  46.    int i;
  47.    int flcount=0;
  48.  
  49.    unsigned long sumalt, nalt;
  50.  
  51.    int patcount = 0;
  52.    char *patfield[256];
  53.  
  54.    char *filename;
  55.  
  56.    sum = 0;
  57.    n = 0;
  58.    pos = 0;                                 /* for tar index files pos = 14 */
  59.    
  60.    for(i=1; i<argc; i++) {
  61.       if(*argv[i]=='-')
  62.          switch(*(argv[i]+1)) {
  63.             case 'p' : pos = 0;
  64.                        sscanf(argv[i]+2,"%d", &pos);
  65.                        break;
  66.             case 'q' : quietflag = 1;
  67.                        break;
  68.             case '?' : printf(usage);
  69.                        exit(0);
  70.                        break;
  71.             case 'h' : printf(usage);
  72.                        exit(0);
  73.                        break;
  74.          }
  75.       else
  76.          patfield[patcount++] = argv[i];
  77.    }
  78.  
  79.    if ( (pos > MAXLEN) || (pos<0) )
  80.       CloseAll("error: <pos> must be [0-255]");
  81.  
  82.  
  83.    ap = (struct AnchorPath*)&myap;
  84.    ap->ap_Strlen = 254;
  85.    ap->ap_BreakBits = 0;
  86.  
  87.    if(patcount == 0)          /* from stdin */
  88.       sumfile("*");
  89.    else
  90.       for( i=0; i<patcount ; i++) {
  91.          if ( MatchFirst(patfield[i], ap) ) {
  92.             printf("no match on %s\n",patfield[i]);
  93.             continue;
  94.          }
  95.  
  96.          do { 
  97.             fib = &ap->ap_Info;
  98.             if( fib->fib_DirEntryType > 0 )  /* exclude dirs */
  99.                continue;
  100.             sumalt = sum;
  101.             nalt = n;
  102.             if(sumfile(myap.ap_Buf)){      /* name and path */
  103.                flcount++;
  104.                if(!quietflag) {
  105.                   printf("%-32s : %11lu in %7lu lines at column %3d\n",
  106.                          myap.ap_Buf, sum-sumalt, n-nalt, pos);
  107.                }
  108.             }
  109.  
  110.          } while (!MatchNext(ap));
  111.  
  112.          MatchEnd(ap);
  113.       }
  114.  
  115.    if(quietflag)
  116.       printf("%lu\n",sum);
  117.    else {
  118.       printf("-----------------------------------------------------------------------------\n");
  119.       printf("%3d file(s) total                : %11lu in %7lu lines at column %3d\n",
  120.               flcount,sum,n,pos);
  121.    }
  122.  
  123.    exit(0);
  124. }
  125.  
  126. int sumfile(char *filename)
  127. {
  128.    unsigned long fs;
  129.    FILE *f=NULL;
  130.  
  131.    if( (f = fopen(filename, "r")) == NULL) {
  132.      printf("error: file %s not found\n",filename);
  133.      return 0;
  134.    }
  135.  
  136.    h[pos] = '\0';
  137.  
  138.    while(fgets(h,MAXLEN,f) != 0) {
  139.       n++;
  140.       fs = 0;
  141.       
  142.       sscanf(&h[pos],"%lu",&fs);
  143.       sum += fs;
  144.       h[pos] = '\0';
  145.  
  146.    }
  147.    fclose(f);
  148.  
  149.    return 1;  
  150. }
  151.  
  152.  
  153.  
  154. void CloseAll(char *s)
  155. {
  156.    if(s != NULL)
  157.       printf("%s\n",s);
  158.    exit(5);
  159. }
  160.